package com.ruoyi.app.interceptor;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.$;
import com.ruoyi.app.controller.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Zhang on 2018/8/26.
 */
@Component
@Slf4j
public abstract class VersionLocalinterceptor extends BaseController implements HandlerInterceptor {
 public  static  final    String    version="/v1";
    @Override
    public boolean preHandle(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, Object handler)  throws Exception {
        if(request.getServletPath().equals("/error")){
            return true;
        }
        if(request.getServletPath().contains("common") ){
            return true;
        }
        if (HttpMethod.OPTIONS.toString().equals(request.getMethod())) {
            return true;
        }
        if(!request.getServletPath().contains(version)&&!request.getServletPath().contains("gov")&&!request.getServletPath().contains("swagger-ui.html")&&!request.getServletPath().contains("springfox-swagger-ui")&&!request.getServletPath().contains("swagger-resources")){
            Map<String,String> versionUrl=new HashMap<>();
            versionUrl.put("ios",sysConfigService.selectConfigByKey("iosDownload"));
            versionUrl.put("android",sysConfigService.selectConfigByKey("anzhuoDownload"));
            renderJson(JSONObject.toJSONString($.error("有新版本发布，请立即更新。",versionUrl)),response);
            return false;
        }
        return true;
    }

}
